package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface SetmealMapper {

    @AutoFill(value = OperationType.INSERT)
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("INSERT INTO setmeal (category_id, name, price, status, description, image, create_time, update_time, create_user, update_user) VALUES" +
            " (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser});")
    void insert(Setmeal setmeal);

    Page<SetmealVO> selectByPage(SetmealPageQueryDTO dto);

    int queryStatusByIds(List<Long> ids);

    void deleteByIds(List<Long> ids);


    @Select("select * from setmeal where id = #{id}")
    Setmeal selectById(Long id);


    @AutoFill(value = OperationType.UPDATE)
    void update(Setmeal setmeal);

    /**
     * 删除分类时查询该分类下有无套餐
     * @param id
     * @return
     */
    @Select("select count(*) from setmeal where category_id = #{id}")
    int selectByCategoryId(Integer id);

    /**
     * 删除菜品时查询有没有被套餐关联
     * @param ids
     * @return
     */
    Integer selectByDishIds(List<Long> ids);

    /**
     * 动态条件查询套餐
     * @param setmeal
     * @return
     */
    List<Setmeal> list(Setmeal setmeal);

    /**
     * 根据套餐id查询菜品选项
     * @param setmealId
     * @return
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);

    /**
     * 根据条件统计套餐数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

}
